package com.mail.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.mapper.PmsProductMapper;
import com.mail.dubbo.mapper.SmsFlashPromotionProductRelationMapper;
import com.mail.model.db.PmsProduct;
import com.mail.model.db.SmsFlashPromotionProductRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @author yan
 * @date 2022/06/21 10:30
 **/
@DubboService
public class SmsFlashPromotionProductRelationApiImpl implements SmsFlashPromotionProductRelationApi {
    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;


    /**
     * @param smsFlashPromotionProductRelation
     */
    @Override
    public void create(List<SmsFlashPromotionProductRelation> smsFlashPromotionProductRelation) {
        for (SmsFlashPromotionProductRelation flashPromotionProductRelation : smsFlashPromotionProductRelation) {
            smsFlashPromotionProductRelationMapper.insert(flashPromotionProductRelation);
        }

    }

    /**
     * @param id
     */
    @Override
    public void delete(Long id) {
        smsFlashPromotionProductRelationMapper.deleteById(id);
    }

    /**
     * @param smsFlashPromotionProductRelation
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page list(SmsFlashPromotionProductRelation smsFlashPromotionProductRelation, Long pageNum, Long pageSize) {
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        if (smsFlashPromotionProductRelation != null) {
            queryWrapper.eq(smsFlashPromotionProductRelation.getFlashPromotionId() != null, SmsFlashPromotionProductRelation::getFlashPromotionId, smsFlashPromotionProductRelation.getFlashPromotionId())
                    .eq(smsFlashPromotionProductRelation.getFlashPromotionSessionId() != null, SmsFlashPromotionProductRelation::getFlashPromotionSessionId, smsFlashPromotionProductRelation.getFlashPromotionSessionId())
                    .orderByDesc(SmsFlashPromotionProductRelation::getSort);

        }
        Page page = new Page<>(pageNum, pageSize);
        Page selectPage = smsFlashPromotionProductRelationMapper.selectPage(page, queryWrapper);
        return selectPage;
    }

    @Override
    public void update(Long id, SmsFlashPromotionProductRelation relation) {
        SmsFlashPromotionProductRelation productRelation = smsFlashPromotionProductRelationMapper.selectById(id);
        if(productRelation==null){
            smsFlashPromotionProductRelationMapper.insert(relation);
        }else{
            smsFlashPromotionProductRelationMapper.updateById(relation);
        }

    }

    @Override
    public SmsFlashPromotionProductRelation getProductionMessage(Long id) {
        SmsFlashPromotionProductRelation smsFlashPromotionProductRelation = smsFlashPromotionProductRelationMapper.selectById(id);
        return smsFlashPromotionProductRelation;
    }

    @Override
    public List<Long> selectList (Long flashPromotionId){
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
        queryWrapper.groupBy(SmsFlashPromotionProductRelation::getFlashPromotionSessionId);
        List<SmsFlashPromotionProductRelation> list = smsFlashPromotionProductRelationMapper.selectList(queryWrapper);
        List<Long> ids = CollUtil.getFieldValues(list, "flashPromotionSessionId", Long.class);
        return ids;
    }

    @Override
    public Integer getProductCount(Long flashPromotionId, Long id) {
        return smsFlashPromotionProductRelationMapper.getProductCount(flashPromotionId,id);
    }
}
